﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjectWebNhac.DAO;
using System.Data;
using ProjectWebNhac.DTO;
namespace ProjectWebNhac.BUS
{
    public class VideoBus
    {
        public VideoBus() { }
        VideoDTO ojbVideo = new VideoDTO();
        Dataprovider dao = new Dataprovider();
        CaSiBus csBus = new CaSiBus();
        public DataTable LayTop8VideoMoinhat()
        {
            return dao.LayDuLieuThamSo("sp_User_LayTop8VideoMoiNhat",
                CommandType.StoredProcedure);
        }

        public DataTable LayTop20VideoHaynhat()
        {
            return dao.LayDuLieuThamSo("sp_User_LayTop20VideoHaynhat",
                CommandType.StoredProcedure);
        }

        public DataTable LayChiTietVideo(int MaVideo)
        {
            return dao.LayDuLieuThamSo("sp_User_LayChiTietVideo",
                CommandType.StoredProcedure, new object[] {"@MaVideo",MaVideo });
        }

        public DataTable LayVideoCungCaSiTheoVideo(int MaVideo)
        {
            DataTable dt = dao.LayDuLieuThamSo("sp_User_LayVideoCungCaSi", CommandType.StoredProcedure, new object[] { "@MaVideo", MaVideo });
            return dt;
        }

        public DataTable LayChiTietCaSiTheoVideo(int MaVideo)
        {
            DataTable dt = dao.LayDuLieuThamSo("sp_User_LayChiTietCaSiTheoVideo", CommandType.StoredProcedure, new object[] { "@MaVideo", MaVideo });
            return dt;
        }

        public bool ThemVideo(VideoDTO ojbVideo)
        {
            bool isSuccess = false;
            isSuccess = dao.XuLySuKien("sp_Admin_ThemVideo", CommandType.StoredProcedure,
               new object[] {"@TenVideo",ojbVideo.TenVideo,"@Timespan ",ojbVideo.Timespan,
                   "@LinkVideo",ojbVideo.LinkVideo,"@MaTheLoai",ojbVideo.MaTheLoai ,"@AvatarClip",ojbVideo.AvatarClip ,});
            if (isSuccess)
            {
                int MaVideo = Convert.ToInt16(dao.LayDuLieuThamSo("sp_Admin_LayMaVideoMoiNhat",
                    CommandType.StoredProcedure).Rows[0][0]);
                foreach (var maCasi in csBus.LayMaCasiTheoTen(ojbVideo.TenCaSi))
                {
                    isSuccess = dao.XuLySuKien("sp_Admin_ThemCaSiVideo", CommandType.StoredProcedure,
                        new object[] { "@MaCaSi", maCasi, "@MaVideo", MaVideo });
                }
            }
            return isSuccess;
        }


        public DataTable LoadDanhSachVideo()
        {
            DataTable dt = dao.LayDuLieuThamSo("sp_Admin_LayDanhSachVideo", CommandType.StoredProcedure);
            return dt;
        }

        public bool SuaVideo(VideoDTO ojbVideo)
        {
            bool isSuccess = false;
            isSuccess = dao.XuLySuKien("sp_Admin_SuaVideo", CommandType.StoredProcedure,
               new object[] {"@MaVideo",ojbVideo.MaVideo,"@TenVideo",ojbVideo.TenVideo,
                   "@MaTheLoai",ojbVideo.MaTheLoai ,"@Timespan",ojbVideo.Timespan});

            //if (isSuccess)
            {
                isSuccess = dao.XuLySuKien("sp_Admin_XoaCaSiVideoTheoMaVideo",
                    CommandType.StoredProcedure, new object[] { "@MaVideo", ojbVideo.MaVideo });
            }
            foreach (var maCasi in csBus.LayMaCasiTheoTen(ojbVideo.TenCaSi))
            {
                isSuccess = dao.XuLySuKien("sp_Admin_ThemCaSiVideo", CommandType.StoredProcedure,
                        new object[] { "@MaCaSi", maCasi, "@MaVideo", ojbVideo.MaVideo });
            }
            return isSuccess;
        }

        public bool XoaVideo(int MaVideo)
        {
            bool isSuccess = dao.XuLySuKien("sp_Admin_XoaVideo", CommandType.StoredProcedure,
               new object[] {"@MaVideo",MaVideo});

            return isSuccess;
        }

        public DataSet LayDanhSachVideoPT(int currPage, int recodperpage, int Pagesize, int MaTheLoai)
        {
            return dao.LayDsThamSo("sp_User_PhanTrangVideo", CommandType.StoredProcedure,
                new object[] { "@currPage", currPage, "@recodperpage", recodperpage, "Pagesize",
                    Pagesize,"@MaTheLoai",MaTheLoai });
        }
        public DataSet TimKiemTheoVideo(int currPage, int recodperpage, int Pagesize, string keyword)
        {
            return dao.LayDsThamSo("sp_User_TimKiemTheoVideo", CommandType.StoredProcedure,
                new object[] { "@currPage", currPage, "@recodperpage", recodperpage, "Pagesize",
                    Pagesize,"@txtSearch",keyword });
        }
    }
}